/**
 * @function 中序遍历迭代法，遍历顺序:左->中->右，压栈顺序:右->中->左
*/

var inorderTraversal = function (root) {
  const res = [],
    stk = []
  if (root) stk.push(root)
  while (stk.length) {
    const node = stk.pop()
    if (!node) {
      res.push(stk.pop().val)
      continue
    }
    if (node.right) stk.push(node.right) // 右节点入栈
    stk.push(node) // 中间节点入栈
    stk.push(null)
    if (node.left) stk.push(node.left) // 左节点入栈
  }
  return res
}

const root = {
  val: 1,
  left: {
    val: 2,
    left: null,
    right: null
  },
  right: {
    val: 3,
    left: null,
    right: null
  }
}

console.log(inorderTraversal(root));
